import {createRouter, createWebHistory, useRoute} from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index'
    },
    {
      path: '/index',
      name: 'index',
      component: () => import('../views/index/Index.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/Login.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register/Register.vue')
    },
    {
      path: '/mine',
      name: 'mine',
      // redirect: '/mine/like',
      component: () => import('../views/mine/Mine.vue'),
      children: [
        {
          path: '/mine/like',
          name: 'mineLike',
          component: () => import('../views/mine/Like.vue')

        },
        {
          path:'/mine/sheet',
          name:'mineSheet',
          component: ()=> import('../views/mine/Sheet.vue'),
        },
        {
          path:'/mine/recent',
          name:'mineRecent',
          component: ()=> import('../views/mine/Recent.vue')
        }
      ]
    },{
      path: '/upload',
      name: 'upload',
      component: () => import('../views/upload/Upload.vue')
    },{
      path: '/apply',
      name: 'apply',
      component: () => import('@/views/apply/Apply.vue')
    },{
      path: '/vip',
      name: 'vip',
      component: () => import('../views/vip/Vip.vue')
    },{
      path: '/together',
      name: 'together',
      component: () => import('../views/together/Together.vue')
    },
    {
      path: '/together-invite/:id',
      name: 'togetherInvite',
      component: () => import('../views/together/TogetherInvite.vue')
    },{
      path: '/test',
      name: 'test',
      component: () => import('../views/test/Test.vue')
    }
    // 私信页面
    ,{
      path: '/message',
      name: 'message',
      component: () => import('../views/message/Message.vue')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/search/Search.vue'),
      children: [
        {
          path: '/search/music',
          name: 'searchMusic',
          component: () => import('../views/search/Music.vue')
        },
        {
          path:'/search/album',
          name:'searchAlbum',
          component: ()=> import('../views/search/Album.vue')
        }
      ]
    },
    // 订单页面
    {
      path: '/order',
      name: 'order',
      component: () => import('../views/order/Order.vue')
    },
    // 评论通知页面
    {
      path: '/notice',
      name: 'notice',
      component: () => import('../views/other/notice/Notice.vue')
    },
    {
      path: '/post',
      children: [
        {
          path: '/detail/:id',
          name: 'postDetail',
          component: () => import('@/views/post/PostDetail.vue')
        },
        {
          path: '/post/:postId?',
          name: 'postPost',
          component: () => import('@/views/post/PostPost.vue')
        },
        // 我的帖子页面
        {
          path: '/post/my',
          name: 'myPost',
          component: () => import('../views/post/PostMine.vue')
        },
      ]
    },

    // 社交页面
    {
      path: '/socialize',
      name: 'socialize',
      component: () => import('../views/socialize/Socialize.vue')
    },
    {
      path: '/songDetail/:id',
      name: 'songDetail',
      component: () => import('../views/songDetail/SongDetail.vue')
    },
    {
      path: '/:catchAll(.*)',
      name: 'notfound',
      component: () => import('../views/error/NotFound.vue')
    }
  ]
})



// 设置路由守卫，在进页面之前，判断有token，才进入页面，否则返回登录页面
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (!token && to.path !== '/login' && to.path !== '/register') {
    next('/login');
  } else {
    next();
  }
})

export default router
